import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter);


// element-ui点击左侧菜单栏中的项，重复点击时会报错，解决这个问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

const routes = [
    {
        path: '/',
		redirect:'/index/news'  //重定向
    }, {
        path: '/login',
        name:'login',
		meta:{title:'登录'},
        component: () => import('../views/login.vue')
    }, {
        path: '/home',
        name:'home',
		redirect:'/index',  //重定向
		meta:{title:''},
        component: () => import('../views/home/index.vue'),
		children:[
			{
				path: '/index',
				name:'index',
				meta:{title:'首页'},
				component: () => import('../views/home/index/index.vue'),
				children:[
					{
						path: '/index/news',
						name:'indexNews',
						meta:{title:'首页内容'},
						component: () => import('../views/home/index/news.vue')
					},
					{
						path: '/index/message',
						name:'indexMessage',
						meta:{title:'详情'},
						component: () => import('../views/home/index/message.vue')
					},
					{
						path: '/index/personal',
						name:'indexPersonal',
						meta:{title:'个人中心'},
						component: () => import('../views/home/index/personal.vue')
					}
				]
			},{
				path: '/djxc',
				name:'djxc',
				meta:{title:'党建宣传'},
				component: () => import('../views/home/djxc/index.vue'),
				children:[
					{
						path: '/djxc/open',
						name:'djxcOpen',
						meta:{title:'党建发布'},
						component: () => import('../views/home/djxc/open.vue')
					},{
						path: '/djxc/openDetail',
						name:'openDetail',
						meta:{title:'党建发布详情'},
						component: () => import('../views/home/djxc/openDetail.vue')
					},
					{
						path: '/djxc/release',
						name:'djxcRelease',
						meta:{title:'党务公开'},
						component: () => import('../views/home/djxc/release.vue')
					},{
						path: '/djxc/releaseDetail',
						name:'releaseDetail',
						meta:{title:'党务公开详情'},
						component: () => import('../views/home/djxc/releaseDetail.vue')
					},
					{
						path: '/djxc/report',
						name:'djxcReport',
						meta:{title:'党务报告'},
						component: () => import('../views/home/djxc/report.vue')
					},
					{
						path: '/djxc/reportDetail',
						name:'reportDetail',
						meta:{title:'党务报告详情'},
						component: () => import('../views/home/djxc/reportDetail.vue')
					}
				]
			},{
				path: '/ykt',
				name:'ykt',
				meta:{title:'云课堂'},
				component: () => import('../views/home/ykt/index.vue'),
				children:[
					{
						path: '/ykt/study',
						name:'yktStudy',
						meta:{title:'在线学习'}, 
						component: () => import('../views/home/ykt/study/index.vue')
					},
					{
						path: '/ykt/studyDetail',
						name:'studyDetail',
						meta:{title:'课程详情'}, 
						component: () => import('../views/home/ykt/study/studyDetail.vue')
					},
					{
						path: '/ykt/studyDetail2',
						name:'studyDetail',
						meta:{title:'课程详情'}, 
						component: () => import('../views/home/ykt/study/studyDetail2.vue')
					},
					{
						path: '/ykt/test',
						name:'yktTest',
						meta:{title:'一季一考'}, 
						component: () => import('../views/home/ykt/test/index.vue')
					},
					{
						path: '/ykt/getExamination',
						name:'getExamination',
						meta:{title:'考试入口'},
						component: () => import('../views/home/ykt/test/getExamination.vue')
					},
					{
						path: '/ykt/examination',
						name:'examination',
						meta:{title:'考试详情'},
						component: () => import('../views/home/ykt/test/examination.vue')
					},
					{
						path: '/ykt/testDetail',
						name:'testDetail',
						meta:{title:'考试详情'},
						component: () => import('../views/home/ykt/test/testDetail.vue')
					}
				]
			},{
				path: '/djhd',
				name:'djhd',
				meta:{title:'党建活动'},
				component: () => import('../views/home/djhd/index.vue'),
				children:[
					{
						path: '/djhd/stats',
						name:'djhdStats',
						meta:{title:'线上活动'},
						component: () => import('../views/home/djhd/stats.vue')
					},{
						path: '/djhd/detail',
						name:'djhdDetail',
						meta:{title:'活动详情'},
						component: () => import('../views/home/djhd/detail.vue')
					},{
						path: '/djhd/role',
						name:'useRole',
						meta:{title:'角色统计'},
						component: () => import('../views/home/djhd/role/index.vue'),
						children:[
							{
								path: '/djhd/role/administrators',
								name:'administrators',
								meta:{title:'管理员'},
								component: () => import('../views/home/djhd/role/administrators.vue')
							},{
								path: '/djhd/role/member',
								name:'member',
								meta:{title:'会员'},
								component: () => import('../views/home/djhd/role/member.vue')
							}
						]
					}
				]	
			}
		]
    }
]

const router = new VueRouter({
    routes
})

// 前端路由拦截(需要后端支持)
router.beforeEach(function(to,from,next){
	if(!localStorage.getItem('token')){
		if(to.path !== '/login'){
			next('/login')
		}
	}
	next();
})

export default router
